//
//  DBYDestinaDetailViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/29.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "DBYDestinaDetailViewController.h"

@interface DBYDestinaDetailViewController ()<MyCollectionFlowLayoutDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *imageArr;
// 系统CollectionView
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, assign) NSInteger number;

@end

@implementation DBYDestinaDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DBYDestionationFlowLayout *layout = [[DBYDestionationFlowLayout alloc] init];
    // 签订代理
    layout.delegate = self;
    layout.sectionInset = UIEdgeInsetsMake(5 * WID, 5 * WID, 5 * WID, 5 * WID);
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    
    // 创建Collection
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 66 * HEI) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[DBYPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"DBYPhotoCollectionViewCell"];
    self.imageArr = [NSMutableArray array];

    [self getAFNetWorkingData];
    // Do any additional setup after loading the view.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}
#pragma 数据源必须实现方法 ---- 返回Cell样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DBYPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DBYPhotoCollectionViewCell" forIndexPath:indexPath];
    
    DBYPhotoModel *model = [self.imageArr objectAtIndex:indexPath.row];
    cell.photo = model;
    return cell;
}


-(void)getAFNetWorkingData
{
    
    NSLog(@"------------");
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@/photos.json?per_page=20&page=1",self.aModel.myID];
    
    
    
    [AppTools getDataWithUrlStr:url andBlock:^(id result) {

        NSMutableArray *array = result;
        for (NSMutableDictionary *dic in array) {
            DBYPhotoModel *photo = [[DBYPhotoModel alloc] initWithDictionary:dic];
            [self.imageArr addObject:photo];
            
        }
        // 刷新Collection
        [self.collectionView reloadData];
        
    }];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DBYPhotoModel *photo = [self.imageArr objectAtIndex:indexPath.row];
    
    
    CGFloat height = (WIDTH - 30 * WID)/ 2 *((CGFloat)photo.image_height /(CGFloat)photo.image_width);
    
    return CGSizeMake((WIDTH - 30 * WID)/ 2, height);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DBYPhotoViewController *outlandPlacePhotoVC = [[DBYPhotoViewController alloc] init];
    // 传递图片的数组
    outlandPlacePhotoVC.array = self.imageArr;
    
    // 创建x去接收点击的item的数
    NSInteger x;
    
    x = indexPath.item;
    // 传给下一页
    outlandPlacePhotoVC.x = x;
    
    [self presentViewController:outlandPlacePhotoVC animated:YES completion:^{
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
